/*
 * Copyright (C) 2007 Moritz Tuerk <brotkasten@users.sourceforge.net>
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms 
 * of the GNU General Public License as published by the Free Software Foundation; either 
 * version 2 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied 
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License along with 
 *  this program; if not, write to the Free Software Foundation, 
 *  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 */
package mo.chat;

import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;

/**
 * Implements the listener for typed input of the chat panel
 * 
 * @author Moritz Tuerk
 */
public class InputListener implements DocumentListener {
    private class UpdateThread extends Thread {
        private final String text;

        public UpdateThread(final String text) {
            this.text = text;
        }

        @Override
        public void run() {
            try {
                InputListener.this.panel.fireInput(this.text);
            } catch (final Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * saves the chat panel that is used to display the messages and is
     * monitored by this listener
     */
    private ChatPanel panel;

    /**
     * default constructor (does nothing)
     */
    public InputListener() {
    }

    public void changedUpdate(final DocumentEvent e) {
        // nothing
    }

    /**
     * this is called every time input is typed to the chat input panel. It will
     * fire an update of the chat history if a single newline is typed (return
     * is pressed)
     */
    public void insertUpdate(final DocumentEvent e) {
        try {
            final DefaultStyledDocument doc = (DefaultStyledDocument) e
                    .getDocument();
            final String tmp = doc.getText(e.getOffset(), e.getLength());

            if (tmp.equals("\n") && e.getOffset() == doc.getLength() - 1) {
                final String text = doc.getText(0, doc.getLength());
                SwingUtilities.invokeLater(new UpdateThread(text));
            }
        } catch (final BadLocationException ble) {
            ble.printStackTrace();
        }
    }

    public void removeUpdate(final DocumentEvent e) {
        // nothing
    }

    /**
     * is called to pass a reference to the monitored chat panel
     * 
     * @param panel
     *            saves the panel this listener listens to
     */
    public void setChatPanel(final ChatPanel panel) {
        this.panel = panel;
    }
}
